/*
 * TextDocumentDelegate.h  0.4.0 / Dec 04, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#ifndef TEXTDOCUMENTDELEGATE_H
#define TEXTDOCUMENTDELEGATE_H

#include "Cute4jSwing.h"

#include "QObjectDelegate.h"
#include "QTextDocumentAdapter.h"

// TextDocumentDelegate Class. -------------------------------------------------

/*!
 * \brief TextDocument Delegate.
 *
 * \author Slobodan Vrkacevic
 * \version 0.4.0
 */
class CUTE4J_EXPORT TextDocumentDelegate : public QObjectDelegate
{
  Q_OBJECT
protected:
  TextDocumentDelegate(JNIEnv *env, jobject obj, QTextDocumentAdapter *document);
public slots: // native calls
  void clearText(JNIEnv *env);
  jstring getText(JNIEnv *env);
  void setText(JNIEnv *env, jstring text);
  void appendText(JNIEnv *env, jstring text);
  jboolean isUndoAvailable(JNIEnv *env);
  jboolean isRedoAvailable(JNIEnv *env);
  void undo(JNIEnv *env);
  void redo(JNIEnv *env);
  void setUndoRedoEnabled(JNIEnv *env, jboolean enabled);
  void setUndoRedoListening(JNIEnv *env, jboolean listening);
  jboolean isModified(JNIEnv *env);
  void setModified(JNIEnv *env, jboolean modified);
  void print(JNIEnv *env, jobject printer);
  void setUseHighlighter(JNIEnv *env, jboolean useHighlighter);
  void setFormat(JNIEnv *env, jint start, jint length, jobject format);
public: // native calls
  static void initialize(JNIEnv *env, jclass cls);
public: // native callbacks
  void undoAvailable(jboolean available);
  void redoAvailable(jboolean available);
  void highlightBlock(jint blockStart, jstring blockText);
public:
  inline QTextDocumentAdapter *qTextDocument() const;
public:
  static QTextDocumentAdapter *toQDocument(JNIEnv *env, jobject document);
public:
  virtual void destroy(JNIEnv *env);
protected:
  virtual jfieldID delegatePointerFID() const;
public slots:
  void undoAvailableSlot(bool available);
  void redoAvailableSlot(bool available);
protected:
  inline static jmethodID createMID();
private:
  static jfieldID sm_delegateFID;
  static jmethodID sm_createMID;
  static jmethodID sm_undoAvailableMID;
  static jmethodID sm_redoAvailableMID;
  static jmethodID sm_highlightBlockMID;
};

// TextDocumentDelegate :: Inline Member Function. -----------------------------

/*!
 * \brief Returns QTextDocumentAdapter adaptee.
 *
 * \return QTextDocumentAdapter adaptee
 */
inline QTextDocumentAdapter *TextDocumentDelegate::qTextDocument() const
{
  return static_cast<QTextDocumentAdapter *>(qObject());
}

// TextDocumentDelegate :: Protected Static Inline Member Function. ------------

/*!
 * \brief Returns createMID.
 *
 * \return createMID
 */
inline jmethodID TextDocumentDelegate::createMID()
{
  return sm_createMID;
}

#define TEXT_DOCUMENT_CAST(delegate) \
    (reinterpret_cast<TextDocumentDelegate *>(delegate))
#define TEXT_DOCUMENT_DELEGATE TEXT_DOCUMENT_CAST(delegate)

#endif // TEXTDOCUMENTDELEGATE_H
